using System;
using System.Collections.Generic;
using System.Linq;
using Basmus.ReadWrite;

namespace Basmus.Values
{
    public class PropertyReaderWriterBuilder : IBuilder<IReaderWriter<object>>
    {
        public IReaderWriter<object> Build(ValuesContext valuesContext)
        {
            throw new NotImplementedException();
        }

        private class PropertyReaderWriter : IReaderWriter<object>
        {
            private readonly ValuesContext _valuesContext;
            private readonly IBuilder<IElementsReaderWriter<object>> _builder;
            private readonly IGetterSetterBuilder _getterSetterBuilder;
            private readonly IBindingBuilder _bindingBuilder;

            public PropertyReaderWriter(ValuesContext valuesContext, IBuilder<IElementsReaderWriter<object>> builder, 
                IGetterSetterBuilder getterSetterBuilder, IBindingBuilder bindingBuilder)
            {
                _valuesContext = valuesContext;
                _builder = builder;
                _getterSetterBuilder = getterSetterBuilder;
                _bindingBuilder = bindingBuilder;
            }


            public ReadResult<object> Read(string str)
            {
                var objectValuesProvider = new ValuesProvider<object>();
                var stringValues = str.Split(',');
                var elementsReaderWriter = _builder.Build(_valuesContext);
                if (elementsReaderWriter != null)
                {
                    var values = elementsReaderWriter.Read(stringValues);
                    objectValuesProvider.Set(_valuesContext.Name, values);

                    var elements = values == null ? null : values.ToArray();
                    var getterSetter = _getterSetterBuilder.Build(_valuesContext);
                    if (getterSetter != null)
                    {
                        var container = _bindingBuilder.Build(_valuesContext).GetContainer(elements);
                        
                        //getterSetter.Set(target, container);
                    }
                }
                return ReadResult<object>.Error();
            }

            public string Write(object value)
            {
                throw new NotImplementedException();
            }
        }
    }
}